/*
 *  Brno University of Technology
 *
 *  Copyright (C) 2009-2010 by Brno University of Technology and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://spl.utko.feec.vutbr.cz
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package cz.vutbr.feec.utko.ef.terminals;

import cz.vutbr.feec.utko.ef.core.Random;
import cz.vutbr.feec.utko.ef.individuals.ActionTree;

/**
 * The Class IntegerTerminal stands for terminal representing an integer value.
 */
public class IntegerTerminal extends ActionTree {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 8973564539006005329L;
	
	/** The value. */
	private Integer value;

	/** The min value. */
	private final int MIN_VALUE;

	/** The max value. */
	private final int MAX_VALUE;
	
	/**
	 * Instantiates a new integer terminal.
	 */
	public IntegerTerminal() {
		this(Short.MIN_VALUE, Short.MAX_VALUE);
	}

	/**
	 * Instantiates a new short integer terminal.
	 * 
	 * @param min
	 *            the min
	 * @param max
	 *            the max
	 */
	public IntegerTerminal(short min, short max) {
		assert min <= max;
		if(max < min ) {
			throw new RuntimeException("Minimal value must be less or equal the max value");
		}
		this.MIN_VALUE = min;
		this.MAX_VALUE = max;
	}
	
	/**
	 * Instantiates a new integer terminal.
	 * 
	 * @param min
	 *            the min
	 * @param max
	 *            the max
	 */
	public IntegerTerminal(int min, int max) {
		assert min <= max;
		if(max < min ) {
			throw new RuntimeException("Minimal value must be less or equal the max value");
		}
		this.MIN_VALUE = min;
		this.MAX_VALUE = max;
	}

	/* (non-Javadoc)
	 * @see cz.vutbr.feec.utko.ef.individuals.Action#getSymbol()
	 */
	@Override
	public String getSymbol() {
		return "" + value;
	}

	/* (non-Javadoc)
	 * @see cz.vutbr.feec.utko.ef.individuals.Action#getValue(java.lang.Object[])
	 */
	@Override
	public Object getValue(Object... operands) {
		assert operands.length == 0;
		if (isInputParameter() && value == null) {
			throw new RuntimeException(
					"Input parameter has not set the its value.");
		}
		return value;
	}

	/* (non-Javadoc)
	 * @see cz.vutbr.feec.utko.ef.individuals.Action#initialize()
	 */
	@Override
	public void initialize() {
		assert !isInputParameter();
		Random rnd = new Random();
		value = MIN_VALUE + rnd.nextInt(MAX_VALUE - MIN_VALUE + 1);
	}

	/**
	 * Sets the value.
	 * 
	 * @param value
	 *            the new value
	 */
	public void setValue(Integer value) {
		assert isInputParameter();
		this.value = value;
	}
}
